package com.example.demo.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2022-08-27
 * Time:11:55
 */
//@ControllerAdvice
@RestControllerAdvice // 针对controller的通知类（增强类）
public class MyExceptionAdvice {

    @ExceptionHandler(ArithmeticException.class)
    public HashMap<String,Object>  arithmeticExceptionAdvice(ArithmeticException e) {
        HashMap<String,Object> result = new HashMap<>();
        result.put("state", -1);
        result.put("data", null);
        result.put("msg", "算术异常" + e.getMessage());
        return result;
    }

//    @ExceptionHandler(ArithmeticException.class)
//    public HashMap<String,Object>  exceptionAdvice(Exception e) {
//        HashMap<String,Object> result = new HashMap<>();
//        result.put("state", -1);
//        result.put("data", null);
//        result.put("msg", "异常" + e.getMessage());
//        return result;
//    }
}
